package com.pj.viewpager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.pj.viewpager.adapter.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<View> viewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        MyPagerAdapter adapter = new MyPagerAdapter(viewList, MainActivity.this);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity.this, "这是第" + (position + 1) + "个页面", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);

        viewList = new ArrayList<>();

        viewList.add(LayoutInflater.from(MainActivity.this).inflate(R.layout.red, null));
        viewList.add(LayoutInflater.from(MainActivity.this).inflate(R.layout.orange, null));
        viewList.add(LayoutInflater.from(MainActivity.this).inflate(R.layout.green, null));
        viewList.add(LayoutInflater.from(MainActivity.this).inflate(R.layout.blue, null));
    }
}